/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kimo.bl;

import java.util.Set;
import static kimo.bl.zkUtils.*;
import kimo.persistence.entities.Municipality;
import kimo.persistence.entities.Prefecture;
import kimo.persistence.entities.Region;
import labels.LabelsEnum;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;

/**
 *
 * @author a381u296
 */
public class ProdHome extends KimoUI {

    @Wire
    private Listbox producer_region_id;
    @Wire
    private Listbox producer_prefecture_id;
    @Wire
    private Listbox producer_municipality_id;
    @Wire
    private Textbox producer_address_street;
    @Wire
    private Textbox producer_address_number;
    @Wire
    private Textbox producer_address_zip;

    private ListModelList<Region> lmlRegions;
    private ListModelList<Prefecture> lmlPrefectures;
    private ListModelList<Municipality> lmlMunicipalities;


    @Override
    void getFromUI() {

        if (lmlPrefectures.getSelection().isEmpty()) {
            throwWrongValueException(producer_prefecture_id, LabelsEnum.SELECT_PREFECTURE.toString());
        }

        if (lmlMunicipalities.getSelection().isEmpty()) {
            throwWrongValueException(producer_prefecture_id, LabelsEnum.SELECT_MUNICIPALITY.toString());
        } else {
            getProducer().setProducerMunicipalityId(lmlMunicipalities.getSelection().iterator().next());
        }
        
        
        getProducer().setProducerAddressStreet(producer_address_street.getValue());
        getProducer().setProducerAddressNumber(producer_address_number.getValue());
        getProducer().setProducerAddressZip(producer_address_zip.getValue());
        
    }

    
    @Override
    void updateUI() {
        lmlRegions.addToSelection(getProducer()
                .getProducerMunicipalityId()
                .getMunicipalityPrefectureId()
                .getPrefectureRegionId());

        lmlPrefectures.addToSelection(getProducer()
                .getProducerMunicipalityId()
                .getMunicipalityPrefectureId());

        lmlMunicipalities.addToSelection(getProducer()
                .getProducerMunicipalityId());

        setTextboxValue(producer_address_street, getProducer().getProducerAddressStreet());
        setTextboxValue(producer_address_number, getProducer().getProducerAddressNumber());
        setTextboxValue(producer_address_zip, getProducer().getProducerAddressZip());

    }

   
    @Override
     void populateListboxs() {
        // χρειάζεται μόνον το Regiοn καθώς τα άλλα εξαρτώνται απο την τιμή του
        // ακυρο τα φορτωνω ολα
        lmlRegions = new ListModelList<Region>(getKdao().getAllRegions());
        producer_region_id.setModel(lmlRegions);

        lmlPrefectures = new ListModelList<Prefecture>(getKdao().getProducerPrefectures());
        producer_prefecture_id.setModel(lmlPrefectures);

        lmlMunicipalities = new ListModelList<Municipality>(getKdao().getProducerMunicipalities());
        producer_municipality_id.setModel(lmlMunicipalities);

        
     
     }

     
    
    @Listen("onSelect = #producer_region_id")
    public void onRegionSelect() {
        
        Set<Region> regions = lmlRegions.getSelection();
        Region region = regions.iterator().next();

        lmlPrefectures.clearSelection();
        lmlPrefectures.clear();
        lmlPrefectures = new ListModelList<Prefecture>(region.getPrefectureList());
        producer_prefecture_id.setModel(lmlPrefectures);

        lmlPrefectures.addToSelection(region.getPrefectureList().iterator().next());

        lmlMunicipalities.clear();
        producer_municipality_id.clearSelection();

        Set<Prefecture> prefectures = lmlPrefectures.getSelection();
        Prefecture prefecture = prefectures.iterator().next();

        lmlMunicipalities = new ListModelList<Municipality>(prefecture.getMunicipalityList());
        producer_municipality_id.setModel(lmlMunicipalities);
        
        lmlMunicipalities.addToSelection(prefecture.getMunicipalityList().iterator().next());
    }

    @Listen("onSelect = #producer_prefecture_id")
    public void onPrefectureSelect() {

        Set<Prefecture> prefectures = lmlPrefectures.getSelection();
        Prefecture prefecture = prefectures.iterator().next();

        lmlMunicipalities = new ListModelList<Municipality>(prefecture.getMunicipalityList());
        producer_municipality_id.setModel(lmlMunicipalities);
        
        lmlMunicipalities.addToSelection(prefecture.getMunicipalityList().iterator().next());

    }

    @Listen("onClick = #saveProdHomeBtn")
    @Override
    public void onSaveProducerBtnClick() throws Exception {

        super.onSaveProducerBtnClick(); //To change body of generated methods, choose Tools | Templates.
        
        
    }

    @Override
    void clearUI() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
